$(function() {
    $('form').parsley();
    dynamicBinding();
    getAllMaps();
});
//遍历所有maps输出到表格
function getAllMaps() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {title: "序号", data: null,"render": function (data,type,row,meta) {
                return data=Number(meta.row)+Number(1);
            }},
        {title: "名字", data: "name"},
        {title: "坐标", data: "location"},
        {title: "操作", data: "id","orderable": false, // 禁用排序
            "defaultContent": "","render": function (data,type,row,meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateMapsModal'>更新</button>";
                }
                return data = deleteStr+updateStr;}}
    ];
    let url ='maps/getAllMaps';
    dataTableInit(url,columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/maps/deleteMaps');
    //为所有的update按钮绑定update事件
    updateBind(  '#toUpdateMaps', '/maps/updateMaps', false, false, "#updateMapsModal");
    //为所有的add按钮绑定add事件
    addBind( '#toAddMaps', '/maps/addMaps', false, false, "#addMapsModal");
    //为add模态框绑定消失事件-清空模态框中内容

    $('#addMapsModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddMaps").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateMapsModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateMaps").reset();
    });
    
    
    
   /* //为所有的delete按钮绑定delete事件
    deleteBind("#maps-table-body",".data-delete","data-id",'/maps/deleteMaps');
    //为所有的update按钮绑定update事件
    $("#maps-table-body").on('click', ".data-update", function(){
        console.log("updateMaps被点击");
        let _id=$(this).attr('data-id');
        $("#toUpdateMaps").ajaxForm(function () {
            let id = _id;
            let name = $(" #updateName ").val();
            let location = $(" #updateLocation ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/maps/updateMaps',
                data: { id: id,name:name,location:location},
                success: function (result) {
                    if (result.code==200){
                        window.location.reload();
                    }else {
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddMaps").on('click', function(){
        $("#toAddMaps").ajaxForm(function () {
            let name = $(" #addName ").val();
            let location = $(" #addLocation ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/maps/addMaps',
                data: {name:name,location:location},
                success: function (result) {
                    if(result.code==200){
                        window.location.reload();
                    }else{
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });*/
}
